﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace UtilsBase
{
    public class RepairListPager
    {
        private string jsFn = "ChangePage";
        private int RecordCount;
        private int CurrentPage;
        private int pageSize;
        public string JsFn
        {
            set { jsFn = value; }
        }
        public RepairListPager(int RecordCount, int CurrentPage, int pageSize)
        {
            this.RecordCount = RecordCount;
            this.CurrentPage = CurrentPage;
            this.pageSize = pageSize;
        }

        public string Pager()
        {
            if (RecordCount > 0)
            {
                return Page(RecordCount, CurrentPage, pageSize);
            }
            else
            {
                return "";
            }
        }

        private string Page(int sumRecord, int pageIndex, int pageSize)
        {
            string disabled = "disabled='disabled'";
            string pointer = "style='cursor: pointer;'";
            string FirstPage = "<li><a {1} {0}>首页</a></li>";
            string Previous = "<li><a {1} {0}>上一页</a></li>";
            string Next = "<li><a {1} {0}>下一页</a></li> ";
            string LastPage = "<li><a {1} {0}>末页</a></li>";
            string PageButton = " <li><a class='{0}' {1}>{2}</a></li>";
            string PageButtons = string.Empty;
            string Event = "onclick='" + jsFn + "({0})'";
            int page = pageIndex > SumPage ? pageIndex : SumPage;//总页数

            if (pageIndex > 1)//上一页
            {
                FirstPage = string.Format(FirstPage, string.Format(Event, 1), pointer);
                Previous = string.Format(Previous, string.Format(Event, pageIndex - 1), pointer);
            }
            else
            {
                FirstPage = string.Format(FirstPage, "", disabled);
                Previous = string.Format(Previous, "", disabled);
            }

            int pageCount = 0;
            int stratIndex = 1;
            int endIndex = 5;
            if (page > 5)
            {
                if (pageIndex < 3)
                {
                    stratIndex = 1;
                    endIndex = 5;
                }
                else if (pageIndex > page - 2)
                {
                    stratIndex = page - 4;
                    endIndex = page;
                }
                else
                {
                    stratIndex = pageIndex - 2;
                    endIndex = pageIndex + 2;
                }
            }
            for (int i = stratIndex; i <= endIndex; i++)
            {
                if (pageCount == 5)
                {
                    break;
                }
                if (i > 0 && i <= page)
                {
                    pageCount++;
                    if (pageIndex == i)
                    {
                        PageButtons += string.Format(PageButton, "active", "", i);
                    }
                    else
                    {
                        PageButtons += string.Format(PageButton, "", pointer + " " + string.Format(Event, i), i);
                    }
                }
            }

            if (pageIndex < page)//下一页
            {
                LastPage = string.Format(LastPage, string.Format(Event, page), pointer);
                Next = string.Format(Next, string.Format(Event, pageIndex + 1), pointer);
            }
            else
            {
                LastPage = string.Format(LastPage, "", disabled);
                Next = string.Format(Next, "", disabled);
            }

            string pageStr = string.Format(@"<div class='panel-foot tc'>
                    <div class='fr mt10 gray9'> {0}条 共{1}页</div>
                    <ul class='pagination'>{5}</ul>
                    <ul class='pagination'>{3}</ul>
                    <ul class='pagination pagination-group'>
                        {2}
                    </ul>
                    <ul class='pagination'>{4}</ul>
                    <ul class='pagination'>{6}</ul>
                </div>", sumRecord, page, PageButtons, Previous, Next, FirstPage, LastPage);
            return pageStr;
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int SumPage
        {
            get
            {
                int totalPages = 0;
                int _to = RecordCount / pageSize;
                int _p = RecordCount % pageSize;

                totalPages = _to;
                if (_p > 0)
                {
                    totalPages++;
                }
                return totalPages;
            }
        }

        /// <summary>
        /// 返回当前真实页数
        /// </summary>
        public int PageIndex
        {
            get
            {
                int _pageIndex = CurrentPage;
                int totalPages = SumPage;//总页数

                //防止超出索引
                if (_pageIndex < 1)
                {
                    _pageIndex = 1;
                }
                if (_pageIndex >= totalPages)
                {
                    _pageIndex = totalPages;
                }
                return _pageIndex;
            }
        }
    }
}
